package cn.tutu.blockchain.machinebackups.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.tutu.blockchain.common.TypeEnum;
import cn.tutu.blockchain.exception.BusinessException;
import cn.tutu.blockchain.machinebackups.dao.MachineBackupsDao;
import cn.tutu.blockchain.machinebackups.entity.MachineBackups;
import cn.tutu.blockchain.machinebackups.service.MachineBackupsService;
import cn.tutu.blockchain.machinestatus.dao.MachineStatusDao;
import cn.tutu.blockchain.machinestatus.entity.MachineStatus;

@Service
@Transactional
public class MachineBackupsServiceImpl implements MachineBackupsService {

	@Autowired
	private MachineBackupsDao machineBackupsDao;

	@Autowired
	private MachineStatusDao machineStatusDao;

	@Override
	public void create(MachineBackups machineBackups) {
		MachineStatus status = new MachineStatus();
		status.setMinemachineId(machineBackups.getMachineId());
		status.setFileHash(machineBackups.getFileHash());
		status.setType(TypeEnum.MACHINE_FILE_HASH.getCode());
		List<MachineStatus> findByModel = machineStatusDao.findByModel(status);
		if (findByModel.isEmpty()) {
			throw new BusinessException("该矿机此文件信息尚未添加!");
		}
		String statusId = findByModel.get(0).getId();
		MachineBackups backup = machineBackupsDao.findByStatusId(statusId);
		if (null == backup) {
			backup = new MachineBackups();
			backup.setMachineStatusId(statusId);
			backup.setBackupCount(1);
			backup.setMachineBackupsId1(machineBackups.getMachineBackupsId());
			machineBackupsDao.insert(backup);
		} else {

			if (backup.getBackupCount() == 0) {
				// 备份次数为0
				backup.setMachineBackupsId1(machineBackups.getMachineBackupsId());

			} else if (backup.getBackupCount() == 1) {
				// 备份次数为1
				backup.setMachineBackupsId2(machineBackups.getMachineBackupsId());
			}
			backup.setMachineStatusId(statusId);
			backup.setBackupCount(backup.getBackupCount() + 1);
			machineBackupsDao.update(backup);
		}

	}

	@Override
	public List<String> getFileHashByMacId(String machineId) {
		synchronized (this) {
			List<MachineBackups> macList = machineBackupsDao.findFileHashByMacId(machineId);

			List<MachineBackups> removeList = new ArrayList<MachineBackups>();
			// 接口返回的需要备份的文件hash
			List<String> hashList = new ArrayList<String>();

			for (MachineBackups backup : macList) {

				// 修改备份次数
				backup.setBackupCount(backup.getBackupCount() + 1);

				// 修改备份矿机
				if (backup.getBackupCount() == 1) {
					backup.setMachineBackupsId1(machineId);
				} else if (backup.getBackupCount() == 2) {
					// 如果该矿机已经备份过相同的文件则跳过
					if (machineId.equals(backup.getMachineBackupsId1())) {
						removeList.add(backup);
						continue;
					} else {
						backup.setMachineBackupsId2(machineId);
					}
				}
				hashList.add(backup.getFileHash());
			}
			macList.removeAll(removeList);
			if (null != macList && macList.size() > 0) {
				// 修改备份矿机 备份矿机和备份次数
				machineBackupsDao.updateBatch(macList);
			}
			return hashList;
		}

	}

	@Override
	public String getRootHashByMacId(String machineId) {
		// TODO Auto-generated method stub
		MachineBackups rootHash = machineBackupsDao.findRootHashByMacId(machineId);
		return rootHash == null ? null : rootHash.getFileHash();

	}

}
